import { reqGoodsInfo, reqAddOrUpdateShopCart } from "@/api";
import { getUUID } from "@/utils/uuid_token";
const actions = {
    // 获取产品信息的actions
    async getGoodInfo({commit},skuId) {
        let result = await reqGoodsInfo(skuId)
        if (result.code == 200) {
            commit('GETGOODINFO',result.data)
        }
    },
    // 将产品添加到购物车中
    async addOrUpdataShopCart({ commit },{skuId,skuNum}) {
        let result = await reqAddOrUpdateShopCart(skuId, skuNum);
        if (result.code==200) {
            return 'ok'
        } else {
            // 代表加入购物车失败
            return Promise.reject(new Error('faile'));
        }
    }
};
const mutations = {
    GETGOODINFO(state, value) {
        state.goodInfo = value
    }
};
const state = {
    goodInfo: {},
    // 游客临时身份
    uuid_token: getUUID()
};
const getters = {
    // 路径导航简化的数据
    categoryView(state) {
        return state.goodInfo.categoryView||{};
    },
    // 简化产品信息的数据
    skuInfo(state) {
        return state.goodInfo.skuInfo || {};

    },
    // 产品售卖属性的简化
    spuSaleAttrList(state) {
        return state.goodInfo.spuSaleAttrList ||[];
    }
};

export default {
    actions,
    mutations,
    state,
    getters
}